﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using YArchitech.Controls;

namespace StairsModuleAndEdit.StairsModel.AddSigns
{
	internal class AddSignManger
	{
		internal AddSignManger(Document m_doc)
		{
			this.m_doc = m_doc;
			this.signMaker = SignMaker.GetSignMaker(m_doc);
			this.levelFinder = LevelFinder.GetLevelFinder(m_doc);
		}

		internal bool AddOneSign(FamilyInstance input, ref int topCount, Transform useTransForm = null)
		{
			PackerBase packerBase;
			Level topLevel;
			string text;
			try
			{
				packerBase = this.packerFactory(input, useTransForm);
				if (packerBase == null)
				{
					return false;
				}
				topLevel = this.levelFinder.GetTopLevel(packerBase);
				if (topLevel == null)
				{
					topCount++;
					return false;
				}
				string text2;
				text = this.signMaker.MakeTempFamily(packerBase, out text2);
			}
			catch (Exception)
			{
				return false;
			}
			if (!string.IsNullOrEmpty(text) && !string.IsNullOrWhiteSpace(text) && topLevel != null)
			{
				try
				{
					Transaction transaction = new Transaction(this.m_doc, "addSign");
					transaction.Start();
					XYZ point = packerBase.ThisFamilyInstanceLocation.Point;
					double rotation = packerBase.ThisFamilyInstanceLocation.Rotation;
					XYZ xyz = new XYZ(point.X, point.Y, topLevel.Elevation);
					FamilySymbol familySymbol = null;
                    this.m_doc.LoadFamilySymbol(text, StaticRescours.Str_Normal, out familySymbol);
					FamilyInstance familyInstance = null;
					if (familySymbol != null)
					{
						familySymbol.Activate();
						familyInstance = this.m_doc.Create.NewFamilyInstance(xyz, familySymbol, topLevel, 0);
						ElementTransformUtils.RotateElement(this.m_doc, familyInstance.Id, Utility.GetZAix(xyz, true), rotation);
					}
					if (packerBase.UseTransform != null && familyInstance != null)
					{
						XYZ xyz2 = Utility.TransformPoint(point, packerBase.UseTransform);
						XYZ xyz3 = xyz2 - point;
						ElementTransformUtils.MoveElement(this.m_doc, familyInstance.Id, new XYZ(xyz3.X, xyz3.Y, 0.0));
						XYZ xyz4 = new XYZ(xyz2.X, xyz2.Y, xyz.Z);
						int num = 1;
						double axyrotateFromTransform = Utility.GetAXYRotateFromTransform(packerBase.UseTransform, out num);
						Line line = Line.CreateBound(xyz4, new XYZ(xyz4.X, xyz4.Y, xyz4.Z + (double)num));
						ElementTransformUtils.RotateElement(this.m_doc, familyInstance.Id, line, axyrotateFromTransform);
					}
					transaction.Commit();
					this.lstTempFilePath.Add(text);
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			if (topLevel == null)
			{
				YJKMessageBox.Show(StaticRescours.Str_MessageNoHighLevel);
			}
			return false;
		}

		private PackerBase packerFactory(FamilyInstance input, Transform useTransform = null)
		{
			EscalatorPacker escalatorPacker = new EscalatorPacker();
			RampwayPacker rampwayPacker = new RampwayPacker();
			if (escalatorPacker.SetFamilyInstance(input, useTransform))
			{
				return escalatorPacker;
			}
			if (rampwayPacker.SetFamilyInstance(input, useTransform))
			{
				return rampwayPacker;
			}
			return null;
		}

		internal void DeletTempFiles()
		{
			foreach (string path in this.lstTempFilePath)
			{
				try
				{
					File.Delete(path);
				}
				catch (Exception)
				{
				}
			}
			this.lstTempFilePath.Clear();
		}

		private Document m_doc;

		private SignMaker signMaker;

		private LevelFinder levelFinder;

		private List<string> lstTempFilePath = new List<string>();
	}
}
